/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.rest.app.shop.controller.app;

import com.jnzc.ocr.common.bean.Page;
import com.jnzc.ocr.common.bean.PageResult;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.common.vo.UserVo;
import com.jnzc.ocr.dependencies.rest.base.BaseController;
import com.jnzc.ocr.rest.app.shop.param.app.AppMemberSaveParam;
import com.jnzc.ocr.serv.shop.api.ShopMemberApi;
import com.jnzc.ocr.serv.shop.api.ShopOrderApi;
import com.jnzc.ocr.serv.shop.consts.ShopOrderConsts;
import com.jnzc.ocr.serv.shop.entity.ShopMember;
import com.jnzc.ocr.serv.shop.entity.ShopOrder;
import com.jnzc.ocr.serv.shop.filter.ShopMemberFilter;
import com.jnzc.ocr.serv.shop.filter.ShopOrderFilter;
import com.jnzc.ocr.serv.system.api.SysNoApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhangcm
 * @version 1.0, 2017/9/13
 */
@Api(tags = {"app - 会员接口"})
@RestController
@RequestMapping("/app/members")
public class AppMemberController extends BaseController {

    @Autowired
    private ShopMemberApi shopMemberApi;

    @Autowired
    private ShopOrderApi shopOrderApi;

    @ApiOperation(value = "分页")
    @GetMapping("")
    public PageResult<ShopMember> search(Page page){
        UserVo userVo = super.get();
        ShopMemberFilter memberFilter = new ShopMemberFilter();
        memberFilter.setShopId(userVo.getShopId());
        return shopMemberApi.search(memberFilter, page.getPageNo(), page.getPageSize());
    }

    @ApiOperation(value = "详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "会员ID", required = true, paramType = "path")
    })
    @GetMapping("/{id}")
    public ShopMember info(@PathVariable Long id){
        UserVo userVo = super.get();
        return shopMemberApi.find(id, userVo.getShopId());
    }

    @ApiOperation(value = "保存")
    @PostMapping("")
    public void save(@RequestBody AppMemberSaveParam param){
        UserVo userVo = super.get();
        ShopMember obj;
        if(null == param.getId()){
            obj = new ShopMember();
            obj.setCompanyId(userVo.getCompanyId());
            obj.setShopId(userVo.getShopId());
        } else {
            obj = shopMemberApi.find(param.getId(), userVo.getShopId());
        }
        obj.setName(param.getName());
        obj.setAccessType(param.getAccessType());
        obj.setAccessTypeName(null);
        obj.setMemberCategoryId(param.getMemberCategoryId());
        obj.setMemberCategoryName(null);
        obj.setContactWay(param.getContactWay());
        obj.setBirthday(param.getBirthday());
        obj.setPoint(param.getPoint());
        shopMemberApi.save(obj);
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "会员ID", required = true, paramType = "path")
    })
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id){
        UserVo userVo = super.get();
        shopMemberApi.delete(id, userVo.getShopId());
    }

    @ApiOperation(value = "充值记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, paramType = "query")
    })
    @GetMapping("/orderRecharge/search")
    public PageResult<ShopOrder> orderRechargeSearch(Long id, Page page){
        ValidateUtils.isId(id, "会员ID");
        UserVo userVo = super.get();
        ShopOrderFilter filter = new ShopOrderFilter();
        filter.setShopId(userVo.getShopId());
        filter.setMemberId(id);
        filter.setType(ShopOrderConsts.TYPE_RECHARGE);
        filter.setState(ShopOrderConsts.STATE_FINISH);
        filter.setOrderBy("finish_time desc");
        return shopOrderApi.search(filter, page.getPageNo(), page.getPageSize());
    }

    @ApiOperation(value = "消费记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, paramType = "query")
    })
    @GetMapping("/orderSale/search")
    public PageResult<ShopOrder> orderSaleSearch(Long id, Page page){
        ValidateUtils.isId(id, "会员ID");
        UserVo userVo = super.get();
        ShopOrderFilter filter = new ShopOrderFilter();
        filter.setShopId(userVo.getShopId());
        filter.setMemberId(id);
        filter.setType(ShopOrderConsts.TYPE_SALE);
        filter.setState(ShopOrderConsts.STATE_FINISH);
        filter.setOrderBy("finish_time desc");
        return shopOrderApi.search(filter, page.getPageNo(), page.getPageSize());
    }
}
